package com.designschool.base.download;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

/****
 * 
 * @��Ŀ��:base
 * 
 * @����:VideoDao.java
 * 
 * @������:shibaotong
 * 
 * @������:�������������Ϣ
 * 
 * @date:2013-11-20
 * 
 * @Version:1.0
 * 
 ***************************************** 
 */
public class DownloadDao extends SQLiteOpenHelper {

    private static final String NAME = "Download";
    private static final int version = 1;
    private SQLiteDatabase db;
    private List<Downloadinfo> listDownloadinfos;

    public DownloadDao(Context context) {
        super(context, NAME, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table download_info(_id integer PRIMARY KEY AUTOINCREMENT, thread_id integer, "
                + "progess integer, fizesize integer, title varchar(100),url varchar(150),status varchar(10), id varchar(10)" +
                ",start_pos integer, end_pos integer,filepath varchar(150))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    /***
     * ��������Ϣ����
     * 
     * @param downloadinfo
     */
    public void addInfos(List<Downloadinfo> downloadinfos) {
        db = getWritableDatabase();
        for(Downloadinfo downloadinfo:downloadinfos)
        {
        ContentValues values = new ContentValues();
        values.put("thread_id", downloadinfo.thread_id);
        values.put("progess", downloadinfo.progess);
        values.put("fizesize", downloadinfo.fizesize);
        values.put("title", downloadinfo.title);
        values.put("url", downloadinfo.url);
        values.put("status", downloadinfo.status);
        values.put("id", downloadinfo.id);
        values.put("start_pos", downloadinfo.start_pos);
        values.put("end_pos", downloadinfo.end_pos);
        values.put("filepath", downloadinfo.filepath);
        db.insert("download_info", null, values);
        }
        db.close();
    }

    /***
     * �����Ƿ��һ�γ�ʼ��
     * 
     * @param url
     * @return
     */
    public boolean Findinfo(String url) {
        db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select count(*) from download_info where url=?",
                new String[] { url });
        cursor.moveToFirst();
        int count = cursor.getInt(0);
        cursor.close();
        db.close();
        return count == 0;
    }

    /***
     * ��ȡ������Ϣ
     * @param url
     * @return
     */
    public List<Downloadinfo> getInfos(String url) {
        List<Downloadinfo> downloadinfos = new ArrayList<Downloadinfo>();
        Downloadinfo downloadinfo;
        db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from download_info where url=?",
                new String[] { url });
        while (cursor.moveToNext()) {
            downloadinfo=new Downloadinfo();
            downloadinfo._id = cursor.getInt(0);
            downloadinfo.thread_id = cursor.getInt(1);
            downloadinfo.progess = cursor.getInt(2);
            downloadinfo.fizesize = cursor.getInt(3);
            downloadinfo.title = cursor.getString(4);
            downloadinfo.url = cursor.getString(5);
            downloadinfo.status = cursor.getString(6);
            downloadinfo.id = cursor.getString(7);
            downloadinfo.start_pos=cursor.getInt(8);
            downloadinfo.end_pos=cursor.getInt(9);
            downloadinfo.filepath=cursor.getString(10); 
            downloadinfos.add(downloadinfo);
        }
        cursor.close();
        db.close();
        return downloadinfos;
    }
    
    /***
     * ɾ��������Ϣ
     * @param url ���url
     */
    public void Delete(String url)
    {
        db=getWritableDatabase();
        db.delete("download_info", "url=?", new String[] { url });
        db.close();
    }
    
    /***
     * ������ݿ���Ϣ
     * @param url ��ݵ�url
     * @param progess ���µĽ��
     * @param status ���µ�״̬
     */
    public void Update(String url ,int progess ,String status)
    {
        db=getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("progess", progess);
        values.put("status", status);
        db.update("download_info", values, "url=?",  new String[] { url });
        if(status.equals("完毕"))
        {
            db.close();
        }
      
    }
}
